package com.ht.medicaltreatment.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SubjectsAdaptger
import com.ht.medicaltreatment.bean.Page
import com.ht.medicaltreatment.utils.NFCUtils
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.view.ProjectDialog
import com.ht.medicaltreatment.view.ReplaceDialog
import com.ht.medicaltreatment.viewmodel.SubjectsViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_subjects_layout.*

//受试者信息
class SubjectsInfoActivity:BaseActivity(), OnItemClickListener, OnLoadMoreListener,
    View.OnClickListener, OnItemChildClickListener {
    private val model by viewModels<SubjectsViewModel>()
    private lateinit var adapter:SubjectsAdaptger
    private var page = 1
    private var code = ""
    private var projectId = ""
    private lateinit var replaceDialog: ReplaceDialog
    private var isNFC = false

    override fun getLayoutId() = R.layout.activity_subjects_layout

    override fun init() {
        setTitle("受试者列表")
        refreshLayout.setOnLoadMoreListener(this)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = SubjectsAdaptger()
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        adapter.setOnItemChildClickListener(this)
        llProject.setOnClickListener(this)
        btnQuery.setOnClickListener(this)
        initData()

        replaceDialog = ReplaceDialog(this)

        if(Build.MODEL == "V9100"){
            ScanUtils.init()
        }else{
            isNFC = packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
            if(isNFC){
                NFCUtils(this)
            }
        }
    }

    fun initData(){
        showProgress()
        model.requestFormList(page,code,projectId,{
            dismissProgress()
            if(page == 1){
                adapter.setNewInstance(it)
            }else{
                adapter.addData(it?: arrayListOf())
            }
            refreshLayout.finishLoadMore()
        },{
            dismissProgress()
            refreshLayout.finishLoadMore()
            showToast(it)
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as Page
        val intent = Intent(this,CollectionRecordActivity::class.java)
        intent.putExtra("patientId",item.id)
        startActivity(intent)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        initData()
    }

    override fun onClick(v: View?) {
        when(v){
            llProject -> {
                var id = -1
                if(projectId.isEmpty()){
                    id = -1
                }else{
                    id = projectId.toInt()
                }
                ProjectDialog(this,id).show { id, name ->
                    tvProject.text = name
                    projectId = "$id"
                }
            }

            btnQuery -> {
                code = etCode.text.toString()
                page = 1
                KeyboardUtils.hideSoftInput(etCode)
                initData()
            }
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as Page
        replaceDialog.showDialog(-1){name,id ->

        }
    }
}
